package cn.songtx.iot.controller;

import cn.songtx.iot.pojo.Result;
import cn.songtx.iot.pojo.SysFamily;
import cn.songtx.iot.service.ISysFamilyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/family")
@RequiredArgsConstructor
public class SysFamilyController {

    private final ISysFamilyService familyService;
    private final SysUserFamilyController sysUserFamilyController;

    /**
     * 家庭管理:创建Family,增、删、改、查
     * 写入家庭名,描述,本人在user表中的id,户主,做非空判断
     * 不检测重复,可以一直创建,不断生成新的主键id
     * 同步创建家庭-户主关系,对应的userId,familyId
     */
    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public Result familySave(String familyName, String information, Long sysUserId) {
        if (familyName != null && information != null && sysUserId != null) {
            SysFamily sysFamily = new SysFamily();
            sysFamily.setFamilyName(familyName);
            sysFamily.setInformation(information);
            sysFamily.setSysUserId(sysUserId);
            sysFamily.setCreateTime(LocalDateTime.now());
            boolean saved = familyService.save(sysFamily);
            if (saved) {
                return sysUserFamilyController.userFamilySave(sysUserId, sysFamily.getId());
            }
        }
        return Result.error("数据不全,无法创建");
    }

    /**
     * 家庭管理:逻辑删除
     * 先删除家庭对应的家庭关系,再删除家庭
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)
    public Result familyDelete(@PathVariable("id") Long id) {
        boolean useFamilyRemoveByFamilyId = sysUserFamilyController.useFamilyRemoveByFamilyId(id);
        if (useFamilyRemoveByFamilyId) {
            boolean removed = familyService.removeById(id);
            return Result.success(removed ? "家庭删除成功" : "家庭删除失败");
        }
        return Result.success("家庭操作失败");
    }

    /**
     * 家庭管理:根据id,修改家庭名称和描述
     */
    @RequestMapping(value = "/updateFamily", method = RequestMethod.GET)
    public Result familyUpdate(Long id, String familyName, String information) {
        if (id != null) {
            SysFamily sysFamily = new SysFamily();
            sysFamily.setId(id);
            if (familyName != null) {
                sysFamily.setFamilyName(familyName);
            }
            if (information != null) {
                sysFamily.setInformation(information);
            }
            sysFamily.setUpdateTime(LocalDateTime.now());
            boolean updated = familyService.updateById(sysFamily);
            return Result.success(updated ? "修改成功" : "修改失败");
        }
        return Result.error("主键不能为空");
    }

    /**
     * 家庭管理:管理员查询家庭
     * 返回sys_family表的数据,为下一步删除和修改做准备
     */
    @RequestMapping(value = "/listFamily", method = RequestMethod.GET)
    public Result familyList(Long sysUserId) {
        LambdaQueryWrapper<SysFamily> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysFamily::getSysUserId, sysUserId);
        List<SysFamily> list = familyService.list(lambdaQueryWrapper);
        return Result.success(list);
    }
}
